﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_QuanLyQuangCao : System.Web.UI.Page
{
    DaiLyCtr daiLyCtr = new DaiLyCtr();
    EDaiLy daiLy = new EDaiLy();
    protected void checkAlreadyLogin()
    {
        if ( Session["UserID"].ToString() == null)
        {
            Response.Redirect("Login.aspx");
        }
    }
    protected void lienKetDuLieu()
    {
        MultiView1.ActiveViewIndex = 0;
        hdpage.Value = "0";
        if (daiLyCtr.layDaiLy().Rows.Count > 0)
        {
            btTaoMoi.Visible = true;
            btChinhSua.Visible = true;
        }
        else
        {
            btTaoMoi.Visible = true;
            btChinhSua.Visible = false;
        }
        FormView1.DataSource = daiLyCtr.layDaiLy();
        FormView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            checkAlreadyLogin();
            lienKetDuLieu();
        }


    }
    
    protected void btthem_Click(object sender, EventArgs e)
    {
        lbloidungluong.Text = "";
        if (txtnoidung.Text == "")
        {
            lbloinoidung.Text = "*";
        }
        if (txttenqc.Text == "")
        {
            lbloiTenQC.Text = "*";
        }
        HttpPostedFile myFile = txtimage.PostedFile;
        int nFileLen = myFile.ContentLength;
        if (nFileLen > 5242880)
        {
            lbloidungluong.Text = "File quá lớn, Cần <5MB"; //Giới hạn file
            MultiView1.ActiveViewIndex = 1;
        }
        else
        {
            EDaiLy daiLy = new EDaiLy();
            daiLy.MaNV = 1;
            daiLy.Noidung = txtnoidung.Text;
            daiLy.TenDaiLy = txttenqc.Text;
            daiLy.HinhAnh = txtimage.FileName;
            bool flag = daiLyCtr.InsertDaiLyH(daiLy);
            txtimage.PostedFile.SaveAs(Server.MapPath("../Admin/images/QC/") + txtimage.FileName);
            if (flag)
            {
                Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Thêm Đại Lý Thành Công');window.location=\"QuanLyDaiLy.aspx\"</script>");

            }
            else
            {
                Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Thêm Đại Lý Thất Bại');window.location=\"QuanLyDaiLy.aspx\"</script>");
            }

            //MultiView1.ActiveViewIndex = 0;
            //lienKetDuLieu();
        }

    }
    protected void btcapnhat_Click(object sender, EventArgs e)
    {
        if (txtnoidung.Text == "")
        {
            lbloinoidung.Text = "*";
        }
        if (txttenqc.Text == "")
        {
            lbloiTenQC.Text = "*";
        }
        if (!String.IsNullOrEmpty(txtimage.FileName))
        {
            HttpPostedFile myFile = txtimage.PostedFile;
            int nFileLen = myFile.ContentLength;
            if (nFileLen > 5242880)
            {
                lbloidungluong.Text = "File quá lớn,Cần <5MB";//Giới hạn file
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                daiLy.MaDaiLy = int.Parse(hdmalh.Value);
                daiLy.Noidung = txtnoidung.Text;
                daiLy.TenDaiLy = txttenqc.Text;
                daiLy.HinhAnh = txtimage.FileName;
                bool kq = daiLyCtr.UpdateDaiLyH(daiLy);
                txtimage.PostedFile.SaveAs(Server.MapPath("../Admin/images/QC/") + txtimage.FileName);
                imgHinh.ImageUrl = "../Admin/images/QC/" + txtimage.FileName;
                if (kq)
                {

                    Response.Write("<script >alert('Cập nhật đại lý thành công');window.location=\"QuanLyDaiLy.aspx\"</script>");
                }
                else
                {
                    Response.Write("<script >alert('Cập nhật đại lý thất bại!');window.location=\"QuanLyDaiLy.aspx\"</script>");
                }

            }
        }
        else
        {
            imgHinh.ImageUrl = "../Admin/images/QC/" + hdimage.Value;
            daiLy.MaDaiLy = int.Parse(hdmalh.Value);
            daiLy.Noidung = txtnoidung.Text;
            daiLy.TenDaiLy = txttenqc.Text;
            daiLy.HinhAnh = hdimage.Value;
            bool kq = daiLyCtr.UpdateDaiLyH(daiLy);
             if (kq)
             {

                 Response.Write("<script >alert('Cập nhật đại lý thành công');window.location=\"QuanLyDaiLy.aspx\"</script>");
             }
             else
             {
                 Response.Write("<script >alert('Cập nhật đại lý thất bại!');window.location=\"QuanLyDaiLy.aspx\"</script>");
             }
        }
        //MultiView1.ActiveViewIndex = 0;
        //lienKetDuLieu();
        
           
        
    }
    
    
    protected void btChinhSua_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        btthem.Visible = false;
        btcapnhat.Visible = true;
        imgHinh.Visible = true;
        regularFileHinh3.Visible = false;
        txtnoidung.Text = daiLyCtr.layDaiLy().Rows[int.Parse(hdpage.Value)][3].ToString();
        imgHinh.ImageUrl = "../Admin/images/QC/" + daiLyCtr.layDaiLy().Rows[int.Parse(hdpage.Value)][2].ToString();
        txttenqc.Text = daiLyCtr.layDaiLy().Rows[int.Parse(hdpage.Value)][1].ToString();
        hdmalh.Value = daiLyCtr.layDaiLy().Rows[int.Parse(hdpage.Value)][0].ToString();
        hdimage.Value = daiLyCtr.layDaiLy().Rows[int.Parse(hdpage.Value)][2].ToString();
    }
    protected void btTaoMoi_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        btcapnhat.Visible = false;
        btthem.Visible = true;
        txtnoidung.Text = "";
        txttenqc.Text = "";
        imgHinh.Visible = false;
        regularFileHinh3.Visible = true;
    }
    protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {
        FormView1.PageIndex = e.NewPageIndex;
        lienKetDuLieu();
        hdpage.Value = FormView1.PageIndex.ToString();
    }
}